﻿using System;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web;
using Telerik.Web.UI;
using portfolio.Common;
using portfolio.Business;

namespace portfolio.Views
{
    public partial class DancersDB : System.Web.UI.UserControl
    {
        private string LoadedDancersDBScreenControlName
        {
            get
            {
                return (string)ViewState[SessionConstants.LoadedDancersDBScreenControlName] ?? StringConstants.DefaultDancersDBScreenContentControl;
            }

            set
            {
                ViewState[SessionConstants.LoadedDancersDBScreenControlName] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            PopulateDancersDBWindowsSelection();
        }

        protected void PopulateDancersDBWindowsSelection()
        {

            var control = LoadControl(StringConstants.DancersDBScreenViewsFolder + LoadedDancersDBScreenControlName);
            control.ID = StringConstants.GenericDancersDBControlID;
            DancersDBContent.Controls.Clear();
            DancersDBContent.Controls.Add(control);
        }

        protected void btnUserAdminSwitch_Click(object sender, EventArgs e)
        {
            DancersDBBL dancersDBBL = new DancersDBBL();
            LoadedDancersDBScreenControlName = dancersDBBL.GetDancersDBScreenForValue(btnUserAdminSwitch.SelectedToggleState.Value);
            PopulateDancersDBWindowsSelection();
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            DancersDBBL dancersDBBL = new DancersDBBL();
            if (Session[SessionConstants.LoggedIn] == null || !(bool)Session[SessionConstants.LoggedIn]) // Attempt Log In
            {
                txtUserName.Text = string.Empty;
                RadAjaxManager.GetCurrent(Page).ResponseScripts.Add(String.Format("$find('{0}').show();", rwLogin.ClientID));
            }
            else
            {
                Session[SessionConstants.LoggedIn] = false;
                LoadedDancersDBScreenControlName = dancersDBBL.GetDancersDBScreenForValue(StringConstants.Playlist);
                PopulateDancersDBWindowsSelection();
                btnUserAdminSwitch.Visible = false;
                btnLogin.Text = "Login";
            }
        }

        protected void btnCheckLogin_Click(object sender, EventArgs e)
        {
            DancersDBBL dancersDBBL = new DancersDBBL();
            if (Session[SessionConstants.LoggedIn] == null || !(bool)Session[SessionConstants.LoggedIn]) // Attempt Log In
            {
                Session[SessionConstants.LoggedIn] = dancersDBBL.Login(txtUserName.Text.ToLower(), txtPassword.Text);
                if ((bool)Session[SessionConstants.LoggedIn])
                {
                    btnLogin.Text = "Log Out";
                    btnUserAdminSwitch.Visible = true;
                    btnUserAdminSwitch.ToggleStates[0].Selected = true;
                }
                else
                {
                    rwmLoginError.RadAlert("The entered username and/or password was invalid", 330, 180, "Login Failed", "");
                }
            }
        }

        protected void btnHelp_Click(object sender, EventArgs e)
        {
            RadAjaxManager.GetCurrent(Page).ResponseScripts.Add(String.Format("$find('{0}').show();", rwHelp.ClientID));
        }
    }
}